package com.example.dangerouscargodriver.widget;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;

import com.example.dangerouscargodriver.R;
import com.example.dangerouscargodriver.utils.DialogUtils;

import java.util.ArrayList;
import java.util.List;

public class ChooseItemDialog {

    private AlertDialog mDialog = null;
    private Context mContext = null;
    private AlertDialogBtnClickListener mListener = null;

    private TextView mMsgTV = null;
    private TextView mTitleTV = null;
    private LinearLayout mLayoutItemContent = null;
    private View mRootView = null;

    private List<Integer> mSelect = null;

    public ChooseItemDialog(Context context,
                            String title,
                            String msg,
                            List<String> item,
                            String positiveText,
                            String negativeText){
        mContext = context;
        mSelect = new ArrayList<>();

        mRootView = LayoutInflater.from(mContext).inflate(R.layout.layout_choose_item_alert_dialog, null);

        TextView positiveButton = mRootView.findViewById(R.id.alert_dialog_positive_text);
        TextView negativeButton = mRootView.findViewById(R.id.alert_dialog_negative_text);
        mTitleTV = mRootView.findViewById(R.id.alert_dialog_title);
        mTitleTV.setText(title);
        mMsgTV = mRootView.findViewById(R.id.alert_dialog_msg);
        mMsgTV.setText(msg);
        positiveButton.setText(positiveText);
        negativeButton.setText(negativeText);
        positiveButton.setOnClickListener(v -> {
            if(mListener != null) {
                mListener.clickPositive(mSelect);
            }
            mDialog.dismiss();
        });
        negativeButton.setOnClickListener(v -> {
            if(mListener != null) {
                mListener.clickNegative();
            }
            mDialog.dismiss();
        });
        mLayoutItemContent = mRootView.findViewById(R.id.layout_item_content);
        mLayoutItemContent.removeAllViews();
        for(int index = 0; index < item.size(); index++){
            final int selectIndex = index;
            View itemView = LayoutInflater.from(mContext).inflate(R.layout.layout_choose_item, null);
            TextView textView = itemView.findViewById(R.id.tv_item_string);
            textView.setText(item.get(index));
            CheckBox checkBox = itemView.findViewById(R.id.ck_choose_item);
            checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(isChecked){
                        mSelect.add(selectIndex);
                    }
                    else{
                        for(int i = 0; i < mSelect.size(); i++){
                            if(mSelect.get(i) == selectIndex){
                                mSelect.remove(i);
                                break;
                            }
                        }
                    }
                }
            });
            mLayoutItemContent.addView(itemView);
        }
    }

    public interface AlertDialogBtnClickListener {
        void clickPositive(List<Integer> select);

        void clickNegative();
    }

    public void addClickListener(AlertDialogBtnClickListener listener){
        mListener = listener;
    }

    public void showAlertDialog(boolean cancelableTouchOut) {

        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setView(mRootView);

        builder.setCancelable(true);   //返回键dismiss
        //创建对话框
        mDialog = builder.create();
        mDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);//去掉圆角背景背后的棱角
        mDialog.setCanceledOnTouchOutside(cancelableTouchOut);   //失去焦点dismiss
        mDialog.show();
    }

    public TextView getMsgView(){
        return mMsgTV;
    }
}
